﻿using DeviceFaultPushService.MessagePushService.Communication;
using DeviceFaultPushService.MessagePushService.Models;
using DeviceFaultPushService.SystemService;

namespace DeviceFaultPushService.MessagePushService;

public static class PushService
{
    /// <summary>
    /// 日志委托
    /// </summary>
    public delegate void LogDelegate(string log);
    /// <summary>
    /// 日志事件
    /// </summary>
    public static event LogDelegate LogEvent;
    /// <summary>
    /// 日志
    /// </summary>
    /// <param name="log"></param>
    private static void Log(string log)
    {
        if (LogEvent is not null)
        {
            LogEvent(log);
        }
    }

    /// <summary>
    /// 消息队列
    /// </summary>
    private static Queue<InformationModel> TaskQueue { get; set; }

    /// <summary>
    /// 添加消息
    /// </summary>
    /// <param name="messageType"></param>
    /// <param name="contacts"></param>
    /// <param name="message"></param>
    public static void Add(MessageTypeEnum messageType, List<string> contacts, string message)
    {
        //检查接收人是否存在
        if (contacts is null || contacts.Count < 1)
        {
            return;
        }
        if (GetConfiguration.Configuration.EnablePushService == false)
        {
            return;
        }

        //构建信息
        InformationModel information = new();
        information.Contacts = contacts;
        information.Message = message;
        information.MessageType = messageType;

        //添加队列
        if (TaskQueue is not null)
        {
            TaskQueue.Enqueue(information);
        }
        else
        {
            TaskQueue = new();
            TaskQueue.Enqueue(information);
        }
    }

    //确认服务通讯类型
    static ICommunication _communication = new NullCommunication();

    /// <summary>
    /// 开启推送服务
    /// </summary>
    public static void StartService()
    {
        new Task(() =>
        {
            //配置推送通道
            if (GetConfiguration.Configuration.Passageway == MessageChannelEnum.Socket)
            {
                _communication = new SocketCommunication();
            }
            if (GetConfiguration.Configuration.Passageway == MessageChannelEnum.SerialPort)
            {
                _communication = new SerialPortCommunication();
            }
            //打开通道
            if (_communication.Connect())
            {
                Log($"信息推送服务开启成功");
            }
            else
            {
                Log($"信息推送服务开启失败");
            }

            //开始服务
            while (true)
            {
                //检查队列
                if (TaskQueue is not null && TaskQueue.Count >= 1)
                {
                    //检查连接状态
                    if (_communication.IsConnected)
                    {
                        Log($"信息推送服务连接断开");
                        return;
                    }
                    //获取队列信息
                    InformationModel information = TaskQueue.Dequeue();
                    //处理信息
                    foreach (string item in information.Contacts)
                    {
                        if (information.MessageType == MessageTypeEnum.Sms)
                        {
                            _communication.SendSms(item, information.Message, out _);
                        }
                        if (information.MessageType == MessageTypeEnum.Call)
                        {
                            _communication.SendCall(item, information.Message, out _);
                        }
                        //间隔1秒
                        Thread.Sleep(1100);
                    }
                }
                Thread.Sleep(1000);
            }
        }).Start();
    }
}